function str_replace(old, new, str)
	n = string.len(str)
	c = string.len(old)
	str2 = ""
	for i=0, n-c+1 do
		if substr(str, i, c) == old then
			--print("sim")
			str2 = str2 .. new
			i = i + c
		else
			--print("nao "..substr(str, i, c))
			str2 = str2 .. substr(str, i, 1)
		end
	end

	return str2
end

function substr_count(str, arg)
	n = string.len(str)
	c = string.len(arg)
	count = 0
	for i=0, n do
		if substr(str, i, c) == arg then
			count = count + 1
			i = i + c
		end
	end

	return count
end

function substr(str, start, lenght)
	n = string.len(str)
	return string.sub(str, 1+start, start+lenght)
end

function isnumeric(s)
	if tonumber(s) ~= nil then
		return true
	else
		return false
	end
end

function explode(arg, str)
	t = {}

	n = string.len(str)
	c = string.len(arg)
	u = 0
	i = 0
	t[0] = ""
	while i<n do
		if substr(str, i, c) == arg then
			i = i + c - 1
			u = u + 1
			t[u] = ""
		else
			t[u] = t[u]..substr(str, i, 1)
		end
		i = i + 1
	end

	return t
end

function getcharpot(s)
	num = {}
	num[0] = ""
	num[1] = ""
	n = string.len(s)
	for i=0, n do
		if(isnumeric(substr(s, i, 1))) then
			num[0] = num[0]..substr(s, i, 1)
		else
			num[1] = num[1]..substr(s, i, 1)
		end
	end

	return num
end

function der(s)
	if(substr_count(s, "x")) then
		if(substr_count(s, "^")) then
			e = explode("x^", s)
			print(e[0])
			if(e[1] == "0") then
				return ""
			end
			if(e[0] == "") then
				e[0] = "1"
			end
			if(e[0] == "-") then
				e[0] = "-1"
			end
			bas = {}
			exp = {}
			bas = getcharpot(e[0])
			exp = getcharpot(e[1])
			if(exp[0] == "") then
				exp[0] = 1
			end
			multfrac = (tonumber(bas[0])*tonumber(exp[0]))..bas[1]..exp[1]
			if(isnumeric(e[1])) then
				opfrac = e[1]-1; else opfrac = "("..e[1].."-1)"
			end
			if(opfrac == "1") then
				return multfrac.."x"
			end
			if(opfrac == "0") then
				return multfrac
			else
				return multfrac.."x^"..opfrac
			end
		else
			e = explode("x", s)
			if(e[0] == ""  and e[0] == "-") then
				return str_replace("x", "1", s)
			else
				return str_replace("x", "", s)
			end
		end
	else
		return ""
	end
end

print("P")
print("P")
print(der("2x"))

